__author__ = 'vincent Liu'

import os

_trade_list = []

def GetTradeDatesList():
	"""
	:return: a trading dates list
	"""
	_fdatedir_name = "Z:\\data\\WindDB\\setting\\tradingDates.csv"
	if len(_trade_list)!=0:
		_trade_list.clear()
	if os.path.exists(_fdatedir_name):
		reader=open(_fdatedir_name,'r')
		for line in reader:
			if line[0]!="#":
				_trade_list.append(line[0:8])
		reader.close()
		return _trade_list
	else:
		print("[ERROR]{0} not exists.".format(_fdatedir_name))

def GetNextDate(cdate):
	"""
	:param cdate:current date
	:return the next trading date str of cdate
	"""
	if len(_trade_list) == 0:
		GetTradeDatesList()
	nextone=0
	ndate=''
	for dates in _trade_list:
		if nextone==1:
			ndate=dates
			break
		if dates==cdate:
			nextone=1
	return ndate

def GetLastDate(cdate):
	"""
	:param:current date
	:return the last trading date str of cdate
	"""
	ldate = 0
	if len(_trade_list) == 0:
		GetTradeDatesList()
	for dates in _trade_list:
		if dates==cdate:
			return ldate
		ldate=dates
	return -1

def IsTradingDate(cdate):
	"""
	:param:current date
	:return :bool
	"""
	if len(_trade_list) == 0:
		GetTradeDatesList()
	for dates in _trade_list:
		if dates == cdate:
			return True
	return False

def GetCurrentDate(cdate):
	if len(_trade_list) == 0:
		GetTradeDatesList()
	ldate = ""
	for date in _trade_list:
		if date > cdate:
			return ldate
		elif date==cdate:
			return cdate
		ldate =date
